;(function () {
  function toDou(num) {
    return num < 10 ? '0' + num : '' + num;
  }
  function compareAsc(propertyName) {
    return function(object1, object2) {
        var value1 = object1[propertyName];
        var value2 = object2[propertyName];
        if(value2 < value1) {
            return 1;
        } else if(value2 > value1) {
            return -1;
        } else {
            return 0;
        }
    }
  }
  function getLastDay(year,month)
  {
     var new_year = year;  //取当前的年份
     var new_month = month++;//取下一个月的第一天，方便计算（最后一天不固定）
     if(month>12)      //如果当前大于12月，则年份转到下一年
     {
       new_month -=12;    //月份减
       new_year++;      //年份增
     }
     var new_date = new Date(new_year,new_month,1);        //取当年当月中的第一天
     return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//获取当月最后一天日期
  }
  new Vue({
    el: '#rent-report',
    data: {
      menuLiData: [{name: '集团', code: '0'},{name: '大区', code: '1'},{name: '公司', code: '2'}, {name: '项目', code: '3'}],
      menuAct: '0',
      selectData: [],
      selectAct: {name: '', id: ''},
      onSeason: [{name: '年', code: 'y'},{name: '季', code: 'q'},{name: '月', code: 'm'}],
      onSeasonAct: 'y',
      equityAct: 'n',
      expectAct: 'n',
	  selectNameRent: '集团'
    },
    computed: {
      quarter () {
        var remainderReg, remainderReg1;
        var year = new Date().getFullYear();
        var month = new Date().getMonth() + 1;
        remainderReg = Math.ceil(month/3)*3-3;
        remainderReg1 = Math.ceil(month/3)*3-1;

        var setReg = new Date();
        setReg.setFullYear(setReg.getFullYear(), remainderReg-Math.ceil(month/3)*3, 1);
        setReg.setHours(0,0,0,0)
        var bDate = setReg.getFullYear()+'-'+ toDou(setReg.getMonth()+1)+'-'+ toDou(setReg.getDate())

        var setAdd = new Date();
        var d = getLastDay(year, month-1)
        setAdd.setFullYear(setAdd.getFullYear(), remainderReg1+Math.ceil(month/3)*4, d);
        setAdd.setHours(0,0,0,0)
        var eDate = setAdd.getFullYear()+'-'+ toDou(setAdd.getMonth()+1)+'-'+ toDou(setAdd.getDate())
        return {bDate: bDate, eDate: eDate}
      },
      yearBE () {
        var nowYear = new Date().getFullYear();
        var yearReg = nowYear-2;
        var yearAdd = nowYear+2;
        var bDate = yearReg+'-01-01';
        var eDate = yearAdd+'-12-31';
        return {bDate: bDate, eDate: eDate}
      },
      monthBE () {
        var nowYear = new Date().getFullYear();
        var month = new Date().getMonth() + 1;
        var setReg = new Date();
        setReg.setFullYear(nowYear, new Date().getMonth()-5, 1);
        setReg.setHours(0,0,0,0)
        var bDate = setReg.getFullYear()+'-'+ toDou(setReg.getMonth()+1)+'-'+ toDou(setReg.getDate())

        var setAdd = new Date();
        var d = getLastDay(nowYear, new Date().getMonth()+6)
        var last = new Date();
        last.setFullYear(setAdd.getFullYear(), new Date().getMonth()+7, 0)
        setAdd.setFullYear(setAdd.getFullYear(), new Date().getMonth()+6, last.getDate());
        setAdd.setHours(0,0,0,0)
        var eDate = setAdd.getFullYear()+'-'+ toDou(setAdd.getMonth()+1)+'-'+ toDou(setAdd.getDate())
        return {bDate: bDate, eDate: eDate}
      }
    },
    methods: {
      setMenu (val, item) {
        if (this.menuAct == '2') {
          if (val != '3') {
            if (val != '0') {
              val = 0;
            }
          }
        }
        if (this.menuAct == '3') {
          if (val != '2') {
            if (val != '0') {
              val = 0;
            }
          }
        }
        if (this.menuAct == val) return;
        this.menuAct = val;
		this.selectNameRent = item.name
        this.getMenuData()
      },
      setLrmenu (val) {
        if (this.onSeasonAct == val) return;
        this.onSeasonAct = val;
        this.rentYs()
      },
      getMenuData () {
        if (this.menuAct == '0') {
          hlhttp.get({
            url: 'sub_organizations',
            data: {pid: 0},
            success: res => {
              this.selectData = res;
              $('.rent-report').text(res[0].shortName);
              this.selectAct = {name: res[0].shortName, id: res[0].id}
              this.rentYs()
            }
          })
        }
        if (this.menuAct == '1') {
          hlhttp.get({
            url: 'sub_organizations',
            data: {pid: this.selectAct.id},
            success: res => {
              this.selectData = res;
              $('.rent-report').text('请选择');
              this.selectAct = {name: '', id: ''}
            }
          })
        }
        if (this.menuAct == '2') {
          hlhttp.get({
            url: 'companys',
            success: res => {
              res.map(item => {
                item.type = 'CT'
              })
              this.selectData = res;
              $('.rent-report').text('请选择');
              this.selectAct = {name: '', id: ''}
            }
          })
        }
        if (this.menuAct == '3') {
          hlhttp.get({
            url: 'user/projects',
            success: res => {
              res.map(item => {
                item.type = 'XM'
              })
              this.selectData = res;
              $('.rent-report').text('请选择');
              this.selectAct = {name: '', id: ''}
            }
          })
        }
      },
      //权益口径
      equitySet (val) {
        this.equityAct = this.equityAct == 'n' ? 'y' : 'n';
        this.rentYs()
      },
      // 包括预期
      expectSet (val) {
        this.expectAct = this.expectAct == 'n' ? 'y' : 'n';
        this.rentYs()
      },
      //选择下拉
      selectEdSet (id, name) {
        this.selectAct = {name: name, id: id}
        this.rentYs()
      },
      //统计应收租金
      rentYs () {
        var groupId, areaId, companyId, projectId, beginDate, endDate;
        if (this.menuAct == 0) {
          groupId = this.selectAct.id;
          areaId = '#';
          companyId = '#';
          projectId = '#';
        }
        if (this.menuAct == 1) {
          groupId = '#';
          areaId = this.selectAct.id;
          companyId = '#';
          projectId = '#';
        }
        if (this.menuAct == 2) {
          groupId = '#';
          areaId = '#';
          companyId = this.selectAct.id;
          projectId = '#';
        }
        if (this.menuAct == 3) {
          groupId = '#';
          areaId = '#';
          companyId = '#';
          projectId = this.selectAct.id;
        }
        if (this.onSeasonAct == 'y') {
          beginDate = this.yearBE.bDate;
          endDate = this.yearBE.eDate;
        }
        if (this.onSeasonAct == 'q') {
          beginDate = this.quarter.bDate;
          endDate = this.quarter.eDate;
        }
        if (this.onSeasonAct == 'm') {
          beginDate = this.monthBE.bDate;
          endDate = this.monthBE.eDate;
        }
        hlhttp.get({
          url: 'project/amount_receive',
          data: {
            group_id: groupId,
            area_id: areaId,
            company_id: companyId,
            project_id: projectId,
            dimension: this.onSeasonAct,
            equity: this.equityAct,
            expect: this.expectAct,
            begin_date: beginDate,
            end_date: endDate
          },
          success: ysdata => {
            this.syData(groupId, areaId, companyId, projectId, beginDate, endDate, ysdata)
          }
        })
      },
      //实收
      syData (groupId, areaId, companyId, projectId, beginDate, endDate, ysdata) {
        hlhttp.get({
          url: 'project/amount_payed',
          data: {
            group_id: groupId,
            area_id: areaId,
            company_id: companyId,
            project_id: projectId,
            dimension: this.onSeasonAct,
            equity: this.equityAct,
            expect: this.expectAct,
            begin_date: beginDate,
            end_date: endDate
          },
          success: ssdata => {
            this.mjData(groupId, areaId, companyId, projectId, beginDate, endDate, ysdata, ssdata)
            // var mjdata;
            // this.dataxx(ysdata, ssdata, mjdata)
          }
        })
      },
      //面积
      mjData (groupId, areaId, companyId, projectId, beginDate, endDate, ysdata, ssdata) {
        hlhttp.get({
          url: 'contract/project/rent_acreage',
          data: {
            group_id: groupId,
            area_id: areaId,
            company_id: companyId,
            project_id: projectId,
            dimension: this.onSeasonAct,
            equity: this.equityAct,
            expect: this.expectAct,
            begin_date: beginDate,
            end_date: endDate
          },
          success: mjdata => {
            this.dataxx(ysdata, ssdata, mjdata)
          }
        })
      },
      //数据处理
      dataxx (ysdata, ssdata, mjdata) {
        var yqm = [];
        var cps = [];
        var ssdataCps = [];
        var acreage = []
        if (this.onSeasonAct == 'y') {
          ysdata.years.map(item => {
            yqm.push(item.year)
          })
          yqm.sort()
          yqm.map(item => {
            ysdata.years.map(item1 => {
              if (item == item1.year) {
                cps.push(Number(item1.rentPrice))
              }
            })
            ssdata.years.map(item1 => {
              if (item == item1.year) {
                ssdataCps.push(Number(item1.rentPrice))
              }
            })
            mjdata.years.map(item1 => {
              if (item == item1.year) {
                acreage.push(Number(item1.rentAcreage))
              }
            })
          })
        }
        if (this.onSeasonAct == 'q') {
          ysdata.quarters.map(item => {
            item.sortNumber = hlDateUtil.shellDate(item.quarter+'-1')
            item.syr = item.quarter.split('-')[0]+'-'+toDou(Number(item.quarter.split('-')[1])*3)
          })
          ssdata.quarters.map(item => {
            item.sortNumber = hlDateUtil.shellDate(item.quarter+'-1')
            item.syr = item.quarter.split('-')[0]+'-'+toDou(Number(item.quarter.split('-')[1])*3)
          })
          mjdata.quarters.map(item => {
            item.sortNumber = hlDateUtil.shellDate(item.quarter+'-1')
            item.syr = item.quarter.split('-')[0]+'-'+toDou(Number(item.quarter.split('-')[1])*3)
          })
          ssdata.quarters.sort(compareAsc('sortNumber'))
          ysdata.quarters.sort(compareAsc('sortNumber'))
          mjdata.quarters.sort(compareAsc('sortNumber'))
          ysdata.quarters.map(item => {
            yqm.push(item.syr)
          })
          yqm.map(item => {
            ysdata.quarters.map(item1 => {
              if (item == item1.syr) {
                cps.push(Number(item1.rentPrice))
              }
            })
            ssdata.quarters.map(item1 => {
              if (item == item1.syr) {
                ssdataCps.push(Number(item1.rentPrice))
              }
            })
            mjdata.quarters.map(item1 => {
              if (item == item1.syr) {
                acreage.push(Number(item1.rentAcreage))
              }
            })
          })
        }
        if (this.onSeasonAct == 'm') {
          ysdata.months.map(item => {
            item.sortNumber = hlDateUtil.shellDate(item.month+'-1')
          })
          ssdata.months.map(item => {
            item.sortNumber = hlDateUtil.shellDate(item.month+'-1')
          })
          mjdata.months.map(item => {
            item.sortNumber = hlDateUtil.shellDate(item.month+'-1')
          })
          ysdata.months.sort(compareAsc('sortNumber'))
          ssdata.months.sort(compareAsc('sortNumber'))
          mjdata.months.sort(compareAsc('sortNumber'))
          ssdata.months.map(item => {
            yqm.push(item.month)
          })
          yqm.map(item => {
            ysdata.months.map(item1 => {
              if (item == item1.month) {
                cps.push(Number(item1.rentPrice))
              }
            })
            ssdata.months.map(item1 => {
              if (item == item1.month) {
                ssdataCps.push(Number(item1.rentPrice))
              }
            })
            mjdata.months.map(item1 => {
              if (item == item1.month) {
                acreage.push(Number(item1.rentAcreage))
              }
            })
          })
        }
        this.charts(yqm, cps, ssdataCps, acreage)
      },
      charts (yqm, cps, ssdataCps, acreage) {
        var chart = Highcharts.chart('container-aabb', {
        	chart: {
        		zoomType: 'xy'
        	},
        	title: {
        		text: '租金收入（元）',
            align: 'left',
            style: {
      				fontSize: '14px'
      			}
        	},
        	subtitle: {
            text: '租赁面积(m²)',
            align: 'right',
            verticalAlign: 'top',
            style: {
      				fontSize: '14px'
      			}
        	},
        	xAxis: [{
        		categories: yqm,
        		crosshair: true
        	}],
        	yAxis: [
            { // Secondary yAxis
          		title: {
          			text: '',
          		},
          		labels: {
          			format: '{value}',
          			style: {
          				color: Highcharts.getOptions().colors[1]
          			}
          		},
              min: 0,
          		opposite: true
          	},
            { // Primary yAxis
          		labels: {
          			format: '{value}',
          			style: {
          				color: Highcharts.getOptions().colors[1]
          			}
          		},
          		title: {
          			text: '',
          		},
              min: 0
          	}
          ],
        	tooltip: {
        		shared: true
        	},
        	legend: {

        	},
        	series: [{
        		name: '应收租金',
        		type: 'column',
        		yAxis: 1,
        		data: cps,
        		tooltip: {
        			valueSuffix: ' 元'
        		},
            borderColor: '#93a0e2',
            borderWidth: 0,
            color: '#93a0e2',
            pointPlacement: 0.025
        	}, {
        		name: '实收租金',
        		type: 'column',
        		yAxis: 1,
        		data: ssdataCps,
        		tooltip: {
        			valueSuffix: ' 元'
        		},
            borderColor: '#f9b96b',
            borderWidth: 0,
            color: '#f9b96b',
            pointPlacement: -0.03
        	}, {
        		name: '租赁面积',
        		type: 'line',
        		data: acreage,
        		tooltip: {
        			valueSuffix: 'm²'
        		},
            color: '#76dd93',
            lineWidth: 3
        	}]
        });
      }
    },
    mounted () {
      this.getMenuData()
      this.charts()
      hlSelectUi()
    }
  })
} ())
